<template>
  <dir>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <!-- 搜索区域 -->
      <el-row>
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getOrderList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getOrderList"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 订单列表数据 -->
      <el-table :data="orderList" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="订单编号" prop="order_number"></el-table-column>
        <el-table-column
          label="订单价格"
          prop="order_price"
          width="80px"
        ></el-table-column>
        <el-table-column label="是否付款" prop="order_pay" width="80px">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.order_pay == '0'"
              >未付款</el-tag
            >
            <el-tag type="success" v-else>已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="是否发货"
          prop="is_send"
          width="80px"
        ></el-table-column>
        <el-table-column label="下单时间" prop="create_time" width="170px">
          <template slot-scope="scope">
            {{ scope.row.create_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="140px">
          <template slot-scope="scope">
            <el-button
              icon="el-icon-edit"
              type="primary"
              size="mini"
              @click="showBox(scope.row.order_id)"
            ></el-button>
            <el-button
              icon="el-icon-location"
              type="success"
              size="mini"
              @click="showProgressBox()"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 修改对话框 -->
    <el-dialog
      title="修改地址"
      :visible.sync="addressVisible"
      width="50%"
      @close="addressColsed"
    >
      <el-form
        :model="addressForm"
        :rules="addressFormRules"
        ref="addressFormRef"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader
            :options="citydata"
            v-model="addressForm.address1"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addressVisible = false">取 消</el-button>
        <el-button type="primary" @click="editAddress">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 展示物流进度的对话框 -->
    <el-dialog title="物流进度" :visible.sync="progressVisible" width="50%">
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in progressInfo"
          :key="index"
          :timestamp="activity.time"
        >
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </dir>
</template>

<script>
import citydata from './citydata'
export default {
  created() {
    this.getOrderList()
  },
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10,
      },
      // 订单列表
      orderList: [],
      // 订单总数
      total: 0,
      // 控制修改对话框的显示与隐藏
      addressVisible: false,
      addressForm: {
        address1: [],
        address2: '',
      },
      editForm: {},
      address: '',
      addressFormRules: {
        address1: [
          { required: true, message: '请选择省市区县', trigger: 'blur' },
        ],
        address2: [
          { required: true, message: '请输入详细地址', trigger: 'blur' },
        ],
      },
      citydata,
      // 控制物流对话框的显示与隐藏
      progressVisible: false,
      // 物流信息
      progressInfo: [],
    }
  },
  methods: {
    async getOrderList() {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryInfo,
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取订单列表失败!')
      }

      this.orderList = res.data.goods
      this.total = res.data.total
    },
    // 列表显示数量变化
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrderList()
    },
    // 列表页数变化
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getOrderList()
    },
    // 展示修改地址的对话框
    async showBox(id) {
      const { data: res } = await this.$http.get(`/orders/${id}`)
      this.editForm = res.data
      const address = res.data.consignee_addr.split('-')
      this.addressForm.address2 = address.splice(address.length - 1, 1)[0]
      this.addressForm.address1 = address.splice(0, address.length)
      this.addressVisible = true
    },
    // 修改地址
    editAddress() {
      this.$refs.addressFormRef.validate(async (valid) => {
        if (!valid) return this.$message.error('相关地址为空')

        this.address = this.addressForm.address1.map((item) => item).join('-')
        this.address += '-' + this.addressForm.address2
        console.log(this.address)
        this.editForm.consignee_addr = this.address

        const { data: res } = await this.$http.put(
          `/orders/address/${this.editForm.order_id}`,
          this.editForm
        )

        if (res.meta.status !== 200) return this.$message.error('修改地址失败')

        this.addressVisible = false
        this.$message.success('修改地址成功')
        this.getOrderList()
      })
    },
    // 监听修改对话框的关闭
    addressColsed() {
      this.$refs.addressFormRef.resetFields()
    },
    // 展示物流进度的对话框
    async showProgressBox() {
      const { data: res } = await this.$http.get(`/kuaidi/1106975712662`)
      if (res.meta.status !== 200) {
        return this.$message.error('获取物流进度失败')
      }
      this.progressInfo = res.data
      this.progressVisible = true
      console.log(this.progressInfo)
    },
  },
}
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>